package com.imooc.result;

import com.imooc.enums.BaseEnum;
import com.imooc.enums.CommonEnum;

/**
 * 接口统一封装返回结果
 *
 * @param <T>
 * @author imooc
 * @date 2022-12-19
 */
public class ApiResult<T> {

    /**
     * 编码
     *
     * @mock 200-成功
     */
    private int code;

    /**
     * 返回消息
     *
     * @mock ok
     */
    private String msg;

    /**
     * 返回体
     *
     * @mock json
     */
    public T data;

    private ApiResult() {
    }

    private ApiResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ApiResult<T> ok() {
        return new ApiResult<>(BaseEnum.OK.getCode(), BaseEnum.OK.getMsg(), null);
    }

    public static <T> ApiResult<T> ok(T t) {
        return new ApiResult<>(BaseEnum.OK.getCode(), BaseEnum.OK.getMsg(), t);
    }

    public static <T> ApiResult<T> fail() {
        return new ApiResult<>(BaseEnum.UNKNOWN_ERROR.getCode(), BaseEnum.UNKNOWN_ERROR.getMsg(), null);
    }

    public static <T> ApiResult<T> fail(String msg) {
        return new ApiResult<>(BaseEnum.UNKNOWN_ERROR.getCode(), msg, null);
    }

    public static <T> ApiResult<T> fail(int code, String msg) {
        return new ApiResult<>(code, msg, null);
    }

    public static <T> ApiResult<T> fail(CommonEnum commonEnum) {
        return new ApiResult<>(commonEnum.getCode(), commonEnum.getMsg(), null);
    }

    public static <T> ApiResult<T> fail(CommonEnum commonEnum, T data) {
        return new ApiResult<>(commonEnum.getCode(), commonEnum.getMsg(), data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
